﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF_DependencyProperty_Demo
{
    public class PercentageValidationRule : System.Windows.Controls.ValidationRule
    {
        public PercentageValidationRule() { }
        public override System.Windows.Controls.ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            if (int.TryParse(value.ToString(), out int number))
            {
                if (number >= 0 && number <= 100)
                    return System.Windows.Controls.ValidationResult.ValidResult; // 校验通过

                return new System.Windows.Controls.ValidationResult(false, "值必须在 0 到 100 之间！");
            }

            return new System.Windows.Controls.ValidationResult(false, "请输入一个有效的数字！");
        }
    }
}
